/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GPSTracker;

import javax.microedition.io.file.*;
import java.io.*;
import java.util.*;
import javax.microedition.io.*;

/**
 *
 * @author thomas.s
 */
public class INIFile {

    Vector data;
    
    public class Entry extends Object {
        String name, value;
        Entry( String n, String v ) { name = n; value = v; }
        public boolean equals( Object n ) { return name.equals(n); }
        public String getValue() { return value; }
        public String getName() { return name; }
        public void setValue( String v ) { value=v; }
    }

    INIFile()
    {
        data = new Vector(10,2);
    }

    boolean readLines( DataInputStream is )
    {
        if (is==null) return false;
        byte c=0;
        boolean first=true, eof=false, text=false;
        StringBuffer name = new StringBuffer(20);
        StringBuffer value = new StringBuffer(20);
        while ( !eof )
        {
            try { c=is.readByte(); } catch( IOException e ) { eof=true; }
            if (c=='\n' || eof)
            {
                if (name.length()>0) setValue( name.toString(),value.toString() );
                name = new StringBuffer(20);
                value = new StringBuffer(20);
                first = true;
            }
            if (c=='=') first=false; else
            if (c=='"') text=!text; else
            if ((text && c>=' ') || c>' ') {
                if (first) name.append((char)c); else value.append((char)c);
            }
        }
        return true;
    }


    public boolean read( String name )
    {
        data.removeAllElements();
        
        FileConnection f = null;
        DataInputStream is;

        try{
            f = (FileConnection) Connector.open("file:///filesystem/"+name,Connector.READ);
            readLines(f.openDataInputStream());
            f.close();
        } catch( Exception e ) {  }

        try { if (f!=null) f.close(); } catch( IOException e ) {}

        return true;
    }

    public boolean write( String name )
    {
        FileConnection f =null;
        DataOutputStream os=null;

        try{
            f = (FileConnection) Connector.open("file:///filesystem/"+name);
            if (!f.exists()) f.create(); else f.truncate(0);
            os = f.openDataOutputStream();
        } catch( Exception e ) { os=null; }

        if (os!=null) try {
            for ( int i=0; i<data.size(); i++)
            {
                Entry e = ((Entry)data.elementAt(i));
                os.write( e.getName().getBytes(), 0, e.getName().length() );
                os.writeByte('=');
                os.write( e.getValue().getBytes(), 0, e.getValue().length() );
                os.writeByte('\r');os.writeByte('\n');
            }

        } catch(IOException e) {}
        
        try { if (f!=null) f.close(); } catch( IOException e ) {}

        return true;
    }

    public String getValue( String name, String def )
    {
        for ( int i=0; i<data.size(); i++ )
            if (((Entry)data.elementAt(i)).equals(name))
                return ((Entry)data.elementAt(i)).getValue();
        return def;
    }

   public double getDoubleValue( String name, double def )
    {
        double ret=def;
        for ( int i=0; i<data.size(); i++ )
            if (((Entry)data.elementAt(i)).equals(name))
            {
                try {
                    ret=Double.parseDouble(((Entry)data.elementAt(i)).getValue());
                } catch(Exception e) { ret=def; }
                break;
            }
        return ret;
    }

    public int getIntValue( String name, int def )
    {
        int ret=def;
        for ( int i=0; i<data.size(); i++ )
            if (((Entry)data.elementAt(i)).equals(name))
            {
                try {
                    String v = ((Entry)data.elementAt(i)).getValue().toLowerCase();
                    if (v.length()>2)
                    {
                        byte raw[] = v.getBytes();
                        if (raw[0]=='0' && raw[1]=='x')
                            ret=Integer.parseInt(v.substring(2),16);
                        else
                            ret=Integer.parseInt(v,10);
                    }
                    else
                        ret=Integer.parseInt(v,10);
                } catch(Exception e) { ret=def; }
                break;
            }
        return ret;
    }

   public void setValue( String name, String val )
    {
        for ( int i=0; i<data.size(); i++ )
            if (((Entry)data.elementAt(i)).equals(name))
            {
                ((Entry)data.elementAt(i)).setValue(val);
                return;
            }
        data.addElement( new Entry(name,val) );
    }

}
